﻿using DosTexPrj.Core.Models;
using FluentNHibernate.Mapping;

namespace DosTexPrj.Implement.NHibernate.Mapping
{
    public class OrderMapping : ClassMap<Order>
    {
        public OrderMapping()
        {
            Table("Orders");
            Id(x => x.Id, "OrderID").UnsavedValue(0).GeneratedBy.Identity().Not.Nullable();
            Map(x => x.OrderDate, "OrderDate").Not.Nullable();
            Map(x => x.DeliveryDate, "DeliveryDate").Not.Nullable();
            Map(x => x.Freight, "Freight").Length(500).Not.Nullable();
            Map(x => x.IsDeliveredBefore, "IsDeliveredBefore");
            Map(x => x.NumberOfDateExtensionDelivery, "NumberOfDateExtensionDelivery");
            Map(x => x.IsDeleted).Default("0");
            References<Customer>(x => x.Customer).Column("CustomerID").Not.LazyLoad();
            References<OrderStatus>(x => x.OrderStatus).Column("OrderStatusID").Not.LazyLoad();
            References<DeliveryMethod>(x => x.DeliveryMethod).Column("DeliveryMethodID").Not.LazyLoad();
            References<PaymentMethod>(x => x.PaymentMethod).Column("PaymentMethodID").Not.LazyLoad();
            HasMany<OrderDetail>(x => x.OrderDetails).Cascade.AllDeleteOrphan()
                                                    .KeyColumn("OrderID")
                                                    .AsBag().Not.LazyLoad();
        }
    }
}